<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/5
 * Time: 10:40
 */
namespace app\shopping\controller;

use app\shopping\model\Indent;

class Indentcomm extends Indent{
    function __construct()
    {
        parent::__construct();
    }

    /**
     *
     * @api {get} /shopping/indentcomm  订单操作
     * @apiDescription 获取商品订单
     * @apiGroup shopping
     * @apiVersion 0.1.0
     * @apiParam {string} uid 用户ID
     * @apiParam {string} action get/获取用户订单 join/加入订单  cancel/取消订单
     * @apiParam {string} [commInfo] 当action=join时有效，加入商品的json数据
     * @apiParam {string} [indent_id] 当action=cancel时有效，订单号
     *
     
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */

    public function index(){
        $uid = $_GET["uid"];
        $action = $_GET['action'];

        if(!$this->check->isUidExist($uid)){
            echo echoJson(0, "没有此用户！");
            exit;
        }

        if ($action == 'get') {
            if($this->getIndent($uid)){
                echo echoJson(1, $this->getIndent($uid));
            }else{
                echo echoJson(-1, "暂无订单!");
            }
        } else if ($action == 'join') {
            $commInfo = $_GET["commInfo"];
            !empty($commInfo) ? '' : $commInfo = "{}";
            $commInfo = json_decode($commInfo, true);
            if (is_array($commInfo) && count($commInfo) >= 1) {
                $this->joinIndentComm($uid, $commInfo);
            } else {
                echo echoJson(0, "加入订单失败！-0");
            }
        } else if ($action == 'cancel') {
            $indent_id = $_GET["indent_id"];
            if (!empty($indent_id)) {
                $this->cancelIndentComm($uid, $indent_id);
            } else {
                echo echoJson(0, "删除订单失败！-0");
            }
        }
    }
    
}